#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int minDistance(char *word1,char *word2)
{
    int step = 0;
    int flag = 0;
    int word2_end = strlen(word1) - 1;
    int length = 0;
    if(strlen(word1) == 0)
        return strlen(word2);
    if(strlen(word2) == 0)
        return strlen(word1);

    
    for(int i = strlen(word2) - 1;i >= 0;i--)
    {
        for(int j = word2_end;j >= 0;j--)
        {
            if(word1[j] == word2[i])
            {
                length = word2_end - j;
                step += length;
                word2_end = j - 1;
                flag = 1;
                break;
            }
        }
        if(flag == 0)
        {
            word1[word2_end--] = word2[i];
            step += 1;
        }
        flag = 0;

    }
    return step;
}


int main()
{
    char word1[] = "a";
    char word2[] = "ab";
   int m =  minDistance(word1,word2);
   printf("m = %d\n",m);


    return 0;
}